package engine.data
{
	import engine.GameData;
	import engine.display.newAttack.AttackLogic;
	import engine.flag.ObjType;
	import engine.net.datatype.ByteArrayEx;
	import engine.net.mgr.SystemReturn;
	import engine.scene.SceneQueueManager;
	
	import flash.events.IEventDispatcher;
	import flash.geom.Point;
	
	import newx.BaseObject;
	import newx.utils.debug.IO;
		
	public class Scene extends ObjectData
	{
		private var _role			:RoleVo				=	new RoleVo(new Point());

		/**
		 *属性表数组 
		 */
		private var _proTable:BaseObject				= 	new	BaseObject();
		private var _propCount:uint					=	0;
		/**
		 *表格表数据 
		 */		
		private var _recTable:BaseObject				= 	new	BaseObject();
		private var _recCount:uint						=	0;
		
		
		public function Scene(modelId:uint,target:IEventDispatcher=null)
		{
			super(null,String(modelId),target);			
		}
		
		
		public	function AddObj(objID:Point,type:uint,modelId:uint,count:uint,msg:ByteArrayEx):void
		{
			var tobj:SceneObject;
			
			if(type == ObjType.Player)
			{
				if(objID.equals(Role.ObjID))
				{
					
					//主角
					Role.Type		=	type;
					Role.ModelId	=	modelId;
					Role.setPropertiesFormByteArrayEx(count, msg);
					return;
				}
				else
				{
					//其他玩家，服务器优化后，在进入场景后，其他玩家的信息走自定义消息，不走addobj
				}
			}
			switch(type)
			{
				default:
					if(this.CheckProperties(objID.x+"."+objID.y))
					{
						//假对象已经在了
						tobj = GetProperties(objID.x+"."+objID.y);
					}
					else
					{
						tobj = new SceneObject(objID,type,modelId);
					}
					var i:uint = 0;			
					for (i = 0; i < count; i++)
					{ 
						var index:uint		= msg.readUnsignedShort();
						var pName:String	= ObjectData.ProertyNames[index];
						var pType:uint		= ObjectData.ProertyTypes[index];
						var	data:*			= msg.GetDataByType(pType);
						tobj.HandlePropertiesEx(pName,data);
						
						if(SystemReturn._isShowLog)
							IO.traceCommand(i+","+pType+","+pName+","+data);
					}
					
					HandlePropertiesEx(objID.x+"."+objID.y,tobj);	
				break;
			}
		}
		
		public	function RemoveObj(objID:Point):void
		{
			//客户端退出战斗强制
			if(!AttackLogic.getInstance().fighting)
				return;
			
			var tobj:SceneObject	=	GetObject(objID);
			if(tobj != null)
			{
				DeletePropertiesEx(objID.x + "." +objID.y);
			}
			else
			{
				if(SystemReturn._isShowLog)
					IO.traceCommand("移除对象失败",objID.x + "," +objID.y);
			}
		}
		/**
		 * 根据Point获取对象实例
		 * @return 返回对象实例
		 * 
		 */				
		public	 function GetObject(objID:Point):SceneObject
		{
			if(!CheckProperties(objID.x + "." + objID.y))
			{
				return null;
			}
			return	 GetProperties(objID.x + "." + objID.y) as SceneObject;
		}
		
		/**
		 *	不通过回调直接取属性 
		 * @param val 属性名
		 * @return 属性值
		 * 
		 */		
		
		public	function GePropertyEx(val:String):*
		{
			return		GetProperties(val);
		}
		/**
		 *	不通过回调直接取表格
		 * @param val 表格名
		 * @return 表格对象
		 * 
		 */		
		public	function GetRecordEx(val:String):Record
		{
			return		GetProperties(val) as Record;
		}	
		
		public	function GetView(viewid:int):View
		{
			//视图名临时这么写
			var	viewName:String	= "View" + String(viewid);
			
			if(CheckProperties(viewName))
			{
				return GetProperties(viewName);	
			}						
			return		null;
		}	
		//////////////////////////////////////////////////////////////////////////////
		public function Clear():void
		{			
			//super.clearProperties();
			//清除掉场景对象,视图,表格，其他不能清
			getDataForEach(function (propName:String,val:*):void
			{
				if (val is SceneObject)
				{
					DeletePropertiesEx(propName);				
				}	
//				else if(val is View)
//				{
//					//清掉视图数据，不清视图
//					(val as View).Clear()
//				}
//				else if(val is Record)
//				{
//					//清掉表格数据，不清表格
//					(val as Record).Clear()
//				}	
//				else
//				{
//					DeletePropertiesEx(propName);
//				}
			});
//			GC.gc();
		}
		
		/**
		 * 获取主角级别 
		 * @return 
		 * 
		 */		
		public function get roleLevel():int
		{
			try
			{
				return Role.GetProperties("Level")
			}
			catch(e:*)
			{
				IO.traceError("获取主角信息失败[get playerLevel]");
			}
			return -1;
		}
		
		public	function SetPropertyTable(msg:ByteArrayEx,count:int):void
		{
			_propCount = count;
			
			var i:Number = 0;
			for (i = 0; i < count; i++)
			{
				var proName:String	= msg.readMultiByteEx();	
				var proType:uint	= msg.readUnsignedByte();
				
				_proTable.HandleProperties(String(i),proName);
				_proTable.HandleProperties(String(i + _propCount),proType);
				
				if(SystemReturn._isShowLog)
					IO.traceCommand(i+","+proName+","+proType);
			}
			
		}
		
		public	function SetRecordTable(msg:ByteArrayEx,count:int):void
		{
			_recCount = count;
			// string		strName;	// 表格名称，以0结束的字符串，前面不包含长度
			// _UI16		nCols;		// 列数，最大256
			// _UI08		nColType[];	// 列类型
			// ... 其他表的信息
			
			var i:Number = 0;
			for (i = 0; i < count; i++)
			{
				var	recName:String	= msg.readMultiByteEx();
				var	nCols:uint		= msg.readUnsignedShort();
				var	nColType:Array	= new Array();
				var	j:Number = 0;
				
				for (j = 0; j < nCols; j++)
				{
					nColType[j] = msg.readByte();
				}
				
				_recTable.HandleProperties(String(i),recName);			
				
				//以表名为索引存放表格对象
				var		record:Record	= new Record(recName);
				record.SetColType(nColType);				
				HandlePropertiesEx(recName,record);
				
				if(SystemReturn._isShowLog)
					IO.traceCommand(i+","+recName+","+nCols+","+nColType);
				//				IO.traceLog(recName,nCols);
			}
		}	
		
		public	function GetRecordName(index:uint):String
		{
			return _recTable.GetProperties(String(index));
		}
		
		public function AddObjsForCust(pList:Array):void
		{
			var leng	:	int 	= 	pList.length;
			for(var i : int = 0; i < leng; i+=9)
			{
				AddObjForCust(pList.slice(i , i+9));
			}
		}
		
		public	function AddObjForCust(pList:Array):void
		{
			if(pList[0] == 0)
			{//当没有玩家的时候
				return;
			}
			
			var objID:Point=	pList[0];
		
			if(objID.equals(Role.ObjID))
			{//当是自己的时候
				return;
			}
			
			var pObj:SceneObject;
			if(this.CheckProperties(objID.x+"."+objID.y))
			{
				//假对象已经在了
				pObj = GetProperties(objID.x+"."+objID.y);
			}
			else
			{//pList[11]
				pObj = new SceneObject(objID,ObjType.Player,22);
			}
			
			pObj.HandlePropertiesEx("Name",pList[1]);
			pObj.HandlePropertiesEx("Photo",pList[2]);
			pObj.HandlePropertiesEx("Level",pList[3]);
			pObj.HandlePropertiesEx("Nation",pList[4]);
			pObj.HandlePropertiesEx("ProfType",pList[5]);
			pObj.HandlePropertiesEx("Camp",pList[6]);
			pObj.HandlePropertiesEx("PeerageRank",pList[7]);
			pObj.HandlePropertiesEx("CastleNameID",pList[8]);
			pObj.HandlePropertiesEx("ScenePos",pList[9]);
			pObj.HandlePropertiesEx("PeerageLev",pList[11]);
			pObj.HandlePropertiesEx("ViewStatus",pList[12]);
			
			HandlePropertiesEx(pObj.ObjKey, pObj);
		}
		
		public function delObjForCust(pList:Array):void
		{
			var objID:Point	=	pList[0];
			
			//			DelObjKey(ObjType.Player, objID);
			
			DeletePropertiesEx(objID.x + "." + objID.y);
			
			
		}
		
		//添加obj
		public function AddObjByCustom(pList:Array):void
		{
			//要添加的obj个数
			var objCount	: int = pList[0];
			//单个obj属性个数
			var pCount		: int;
			
			var pObj		: SceneObject;
			var objId		: Point;
			//指针
			var index		: int =	1;
			
			for(var i : int = 0; i < objCount; i++)
			{//对象循环
				pCount	=	(pList[index++] - 1) << 1;
				
				objId	=	pList[index++];
				
				if(isRole(objId))
				{//过滤自己
//					SceneQueueManager.getInstance().addSceneObject(_role);
					
					index += pCount;
					continue;
				}
				
				//合并已经产生过的obj
				if(CheckProperties(objId.x+"."+objId.y))
				{
					//假对象已经在了
					pObj = GetProperties(objId.x+"."+objId.y);
				}
				else
				{
					pObj = new SceneObject(objId,0, 0);
				}
								
				for(var j : int = 0; j < pCount; j+=2)
				{//属性索引，属性值
					pObj.HandlePropertiesEx(ProertyNames[pList[index + j]], pList[index + j + 1]);
				}
				
				HandlePropertiesEx(pObj.ObjKey, pObj);
				
				SceneQueueManager.getInstance().addSceneObject(pObj);
				
				index	+=	pCount;
			}
		}
		
		//根据objId删除obj
		public function DelObjByCustom(pList:Array):void
		{
			var objId	:Point	=	pList[0];
			var objKey	:String	=	objId.x + "." + objId.y;
			
			if(CheckProperties(objKey))
			{
				var tObj : SceneObject =	GetProperties(objKey);
				
				SceneQueueManager.getInstance().delSceneObject(tObj);
				
				DeletePropertiesEx(objId.x + "." + objId.y);
			}
		}
		
		//更新obj属性
		public function UpdataObjByCustom(pList:Array):void
		{
			var obj 		: SceneObject;//	=	GetProperties(objKey);
			var pName		: String;
			if(isRole(pList[0]))
			{//过滤自己
				
//				obj 			=	GameData.Role.playerObj;
//				if(obj)
//				{
//					pName			=	ProertyNames[pList[1]];
//					
//					obj.HandlePropertiesEx(pName, pList[2]);
//					
//					SceneQueueManager.getInstance().updataSceneObject(pName, obj);
//				}
				return;
			}
			
			var objKey : String	=	pList[0].x + "." + pList[0].y;
			
			if(CheckProperties(objKey))
			{//是否有老的数据
				obj 			=	GetProperties(objKey);
				pName			=	ProertyNames[pList[1]];
				obj.HandlePropertiesEx(pName, pList[2]);
				
				SceneQueueManager.getInstance().updataSceneObject(pName, obj);
			}
		}
		
		public function isRole(objId : Point):Boolean
		{
			return _role.ObjID.equals(objId);
		}

		public function get Role():RoleVo
		{
			return _role;
		}
	}
}